/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Piasy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.naodong.shenluntiku.mvp.model.error;


import com.apkfuns.logutils.LogUtils;

import java.io.IOException;


/**
 * Created by Piasy{github.com/Piasy} on 15/7/24.
 *
 * used for Rx network error handling
 * Usage: Observable.subscribe(onNext, RxNetErrorProcessor.NetErrorProcessor)
 * run in the observeOn() thread
 * onErrorReturn run in subscribeOn thread (retrofit run in background thread, not good for
 * error handling)
 *
 * Note: if you handle onError for the net request, than you should call it manually:
 * RxNetErrorProcessor.NetErrorProcessor.call(throwable);
 * Otherwise this method won't be invoked
 */
public class RxNetErrorProcessor {


    public static ApiError tryWithApiError(final Throwable throwable) {
        ApiError apiError = null;
        LogUtils.e(throwable);
        if (throwable instanceof HttpException) {
            final HttpException exception = (HttpException) throwable;
            apiError = exception.getApiError();
        } else if (throwable instanceof org.json.JSONException) {
            apiError = new ApiError(ErrorCode.PARSE_ERROR, "数据异常");
        } else if (throwable instanceof IOException) {
            apiError = new ApiError(ErrorCode.NET_ERROR, "网络异常");
        } else {
            apiError = new ApiError(ErrorCode.UNKNOW_ERROR, "请求失败,请重试");
        }

        return apiError;
    }

}
